﻿// dlg_sip_ctrl.cpp: 实现文件
//

#include "pch.h"
#include "CamCtrl.h"
#include "afxdialogex.h"
#include "dlg_sip_ctrl.h"


// dlg_sip_ctrl 对话框

IMPLEMENT_DYNAMIC(dlg_sip_ctrl, CDialogEx)

dlg_sip_ctrl::dlg_sip_ctrl(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DLG_SIP_CTRL, pParent)
{

}

dlg_sip_ctrl::~dlg_sip_ctrl()
{
}

void dlg_sip_ctrl::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(dlg_sip_ctrl, CDialogEx)
END_MESSAGE_MAP()


int dlg_sip_ctrl::set(struct device_info* info)
{
	((CButton*)GetDlgItem(IDC_CHECK1))->SetCheck(info->sip_server_connect ? TRUE : FALSE);
	((CComboBox*)GetDlgItem(IDC_COMBO_PLATFORM_TYPE))->SetCurSel(info->platform_type);
	((CComboBox*)GetDlgItem(IDC_COMBO_SIP_PROTOCAL))->SetCurSel(info->sip_protocal_type);
	((CComboBox*)GetDlgItem(IDC_COMBO_SIP_PROTOCAL_VERSION))->SetCurSel(info->sip_protocal_version);

	CString str; 
	str.Format("%d", info->sip_local_port);
	GetDlgItem(IDC_EDIT_SIP_LOCAL_PORT)->SetWindowText(str);
	GetDlgItem(IDC_EDIT_SIP_SERVER_ID)->SetWindowText(CString(info->sip_server_id).Left(20));
	GetDlgItem(IDC_EDIT_SIP_SERVER_DOMAIN)->SetWindowText(CString(info->sip_server_domain).Left(10));
	str.Format("%d", info->sip_server_port);
	GetDlgItem(IDC_EDIT_SIP_SERVER_PORT)->SetWindowText(str);
	((CIPAddressCtrl*)GetDlgItem(IDC_EDIT_SIP_SERVER_ADDR))->SetAddress(info->sip_server_ip_addr[0], info->sip_server_ip_addr[1], info->sip_server_ip_addr[2], info->sip_server_ip_addr[3]);
	GetDlgItem(IDC_EDIT_SIP_USER_NAME)->SetWindowText(CString(info->sip_user_name));
	GetDlgItem(IDC_EDIT_SIP_USER_ID)->SetWindowText(CString(info->sip_user_id).Left(20));
	GetDlgItem(IDC_EDIT_SIP_PASSWORD)->SetWindowText(CString(info->sip_password));
	GetDlgItem(IDC_EDIT_SIP_PASSWORD_CONFIRM)->SetWindowText(CString(info->sip_password));

	str.Format("%d", info->sip_register_timeout_second);
	GetDlgItem(IDC_EDIT_REGISTER_TIMEOUT)->SetWindowText(str);
	return 0;
}


int dlg_sip_ctrl::get(struct device_info* info)
{
	info->sip_server_connect  = ((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck() ? TRUE : FALSE;

	info->platform_type        = ((CComboBox*)GetDlgItem(IDC_COMBO_PLATFORM_TYPE))->GetCurSel();
	info->sip_protocal_type    = ((CComboBox*)GetDlgItem(IDC_COMBO_SIP_PROTOCAL))->GetCurSel();
	info->sip_protocal_version = ((CComboBox*)GetDlgItem(IDC_COMBO_SIP_PROTOCAL_VERSION))->GetCurSel();

	CString str;
	GetDlgItem(IDC_EDIT_SIP_LOCAL_PORT)->GetWindowText(str); 
	info->sip_local_port = atoi(str);
	
	GetDlgItem(IDC_EDIT_SIP_SERVER_ID)->GetWindowText(str);
	memcpy(info->sip_server_id,str.GetBuffer(),/*str.GetLength()*/20);

	GetDlgItem(IDC_EDIT_SIP_SERVER_DOMAIN)->GetWindowText(str);
	memcpy(info->sip_server_domain, str.GetBuffer(), /*str.GetLength()*/10);

	GetDlgItem(IDC_EDIT_SIP_SERVER_PORT)->GetWindowText(str);
	info->sip_server_port = atoi(str);

	((CIPAddressCtrl*)GetDlgItem(IDC_EDIT_SIP_SERVER_ADDR))->GetAddress(info->sip_server_ip_addr[0], info->sip_server_ip_addr[1], info->sip_server_ip_addr[2], info->sip_server_ip_addr[3]);
	
	GetDlgItem(IDC_EDIT_SIP_USER_NAME)->GetWindowText(str);
	memcpy(info->sip_user_name, str.GetBuffer(), str.GetLength());

	GetDlgItem(IDC_EDIT_SIP_USER_ID)->GetWindowText(str);
	memcpy(info->sip_user_id, str.GetBuffer(), /*str.GetLength()*/20);

	GetDlgItem(IDC_EDIT_SIP_PASSWORD)->GetWindowText(str);
	memcpy(info->sip_password, str.GetBuffer(), str.GetLength());

	//GetDlgItem(IDC_EDIT_SIP_PASSWORD_CONFIRM)->GetWindowText(CString(info->sip_password));

	GetDlgItem(IDC_EDIT_REGISTER_TIMEOUT)->GetWindowText(str);
	info->sip_register_timeout_second = atoi(str);

	return 0;
}

BOOL dlg_sip_ctrl::valid(void)
{
	CString pas, pas_confirm, servier_id, user_id;
	GetDlgItemText(IDC_EDIT_SIP_PASSWORD, pas);
	GetDlgItemText(IDC_EDIT_SIP_PASSWORD_CONFIRM, pas_confirm);
	GetDlgItemText(IDC_EDIT_SIP_SERVER_ID, servier_id);
	GetDlgItemText(IDC_EDIT_SIP_USER_ID, user_id);
	if (servier_id.GetLength() == 20 && user_id.GetLength() == 20
		&& strcmp(pas, pas_confirm) == 0)
		return TRUE;
	//return FALSE;
	if (servier_id.GetLength() != 20) {
		AfxMessageBox("服务器ID长度应为20位", MB_OK | MB_ICONWARNING, 0);
	}
	if (user_id.GetLength() != 20) {
		AfxMessageBox("用户认证ID长度应为20位", MB_OK | MB_ICONWARNING, 0);
	}
	if (strcmp(pas, pas_confirm) != 0) {
		AfxMessageBox("密码输入不一致", MB_OK | MB_ICONWARNING, 0);
	}
	if (pas == "") {
		AfxMessageBox("请输入密码", MB_OK | MB_ICONWARNING, 0);
	}
	return FALSE;

	//return TRUE;
}


BOOL dlg_sip_ctrl::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	
	((CComboBox*)GetDlgItem(IDC_COMBO_PLATFORM_TYPE))->InsertString(0, "28181");
	((CComboBox*)GetDlgItem(IDC_COMBO_PLATFORM_TYPE))->SetCurSel(0);

	((CComboBox*)GetDlgItem(IDC_COMBO_SIP_PROTOCAL))->InsertString(0, "UDP");
	((CComboBox*)GetDlgItem(IDC_COMBO_SIP_PROTOCAL))->InsertString(1, "TCP");
	((CComboBox*)GetDlgItem(IDC_COMBO_SIP_PROTOCAL))->SetCurSel(0);

	((CComboBox*)GetDlgItem(IDC_COMBO_SIP_PROTOCAL_VERSION))->InsertString(0, "GB/T28181-2016");
	((CComboBox*)GetDlgItem(IDC_COMBO_SIP_PROTOCAL_VERSION))->SetCurSel(0);

	GetDlgItem(IDC_EDIT_SIP_LOCAL_PORT)->SetWindowText("");
	GetDlgItem(IDC_EDIT_SIP_SERVER_ID)->SetWindowText("");
	GetDlgItem(IDC_EDIT_SIP_SERVER_DOMAIN)->SetWindowText("");
	((CIPAddressCtrl*)GetDlgItem(IDC_EDIT_SIP_SERVER_ADDR))->SetAddress(0,0,0,0);
	GetDlgItem(IDC_EDIT_SIP_SERVER_PORT)->SetWindowText("");
	GetDlgItem(IDC_EDIT_SIP_USER_NAME)->SetWindowText("");
	GetDlgItem(IDC_EDIT_SIP_USER_ID)->SetWindowText("");
	GetDlgItem(IDC_EDIT_SIP_PASSWORD)->SetWindowText("123456");
	GetDlgItem(IDC_EDIT_SIP_PASSWORD_CONFIRM)->SetWindowText("123456");
	GetDlgItem(IDC_EDIT_REGISTER_TIMEOUT)->SetWindowText("");

	return TRUE; 
}
